<?php

/*
 * Action - Attack
 * 
 * +3 Cards; +1 Buy
 * 
 * Each other player draws a card, then discards down to 3 cards in hand.
 */

class Margrave extends Card implements iKingdom, iHinterlands, iAction, iAttack {
    
    public function __construct() {
        parent::__construct("Margrave", 5);
    }
    
    public function getDescription() {
        return array(
            "+3 Cards",
            " +1 Buy",
            "Each other player draws a card, then discards down to 3 cards in hand.");
    }
    
    public function play(Game $game, $isRealCard = true) {
        $drawed_cards = $game->drawCards(3);
        $game->addBuys(1);
        ScreenDisplay::addScreenMessage($game, true, "get +1 buy and draw ".nicePrint($drawed_cards), "gets +1 buy and draws ".sizeof($drawed_cards)." cards");
        
        $game->attack($this);
        
        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $drawed_card = $game->drawCards(1, $name);
            ScreenDisplay::addScreenMessage($game, true, "draw ".nicePrint($drawed_card), "draws ".sizeof($drawed_card)." cards", $name);
            
            $playHandOther = $game->getPlayHand($name);
            $playHandSize = sizeof($playHandOther);
            if ($playHandSize > 3) {

                $cards_to_discard = $playHandSize - 3;
                
                $output = MySocket::GetUserReply($game, Input_ChooseMany,  $playHandOther, $cards_to_discard, $cards_to_discard, null, "Choose $cards_to_discard cards to discard", $name, true);
            }
        }

        $game->setPhase(Phase_Sleep);
        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
            foreach ($output as $card_name) {
                $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile, $name);
            }
             ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($output), "discards ". sizeof($output) ." cards", $name);
            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}
?>
